package com.glodon.paas.storage.ali;

import com.aliyun.common.auth.RequestSigner;
import com.aliyun.common.auth.ServiceCredentials;
import com.aliyun.common.comm.ExecutionContext;
import com.aliyun.common.comm.RequestMessage;
import com.aliyun.common.comm.ResponseMessage;
import com.aliyun.common.comm.ServiceClient;
import com.aliyun.common.utils.DateUtil;
import com.aliyun.openservices.ClientException;
import com.aliyun.openservices.HttpMethod;
import com.aliyun.openservices.ServiceException;
import com.aliyun.openservices.oss.OSSException;
import com.aliyun.openservices.oss.internal.OSSErrorResponseHandler;
import com.aliyun.openservices.oss.internal.OSSUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * this class is de-compiled from aliyun SDK
 * @author guojf
 *
 */
public abstract class OSSOperation {
	private URI endpoint;
	private ServiceCredentials credentials;
	private ServiceClient client;

	protected OSSOperation(URI paramURI, ServiceClient paramServiceClient,
			ServiceCredentials paramServiceCredentials) {
		assert ((paramURI != null) && (paramServiceClient != null) && (paramServiceCredentials != null));
		this.endpoint = paramURI;
		this.client = paramServiceClient;
		this.credentials = paramServiceCredentials;
	}

	protected ResponseMessage request(HttpMethod paramHttpMethod,
			String paramString1, String paramString2,
			Map<String, String> paramMap1, Map<String, String> paramMap2,
			InputStream paramInputStream, long paramLong) throws OSSException,
			ClientException {
		assert (paramHttpMethod != null);
		String str = OSSUtils.makeResourcePath(paramString1, paramString2);
		if (paramMap1 == null)
			paramMap1 = new HashMap();
		if (paramMap2 == null)
			paramMap2 = new HashMap();
		paramMap1.put("Date", DateUtil.formatRfc822Date(new Date()));
		if (paramMap1.get("Content-Type") == null)
			paramMap1.put("Content-Type", "");
		RequestMessage localRequestMessage = new RequestMessage();
		localRequestMessage.setEndpoint(this.endpoint);
		localRequestMessage.setResourcePath(str);
		localRequestMessage.setHeaders(paramMap1);
		localRequestMessage.setParameters(paramMap2);
		localRequestMessage.setMethod(paramHttpMethod);
		localRequestMessage.setContent(paramInputStream);
		localRequestMessage.setContentLength(paramLong);
		ExecutionContext localExecutionContext = new ExecutionContext();
		localExecutionContext.setCharset("utf-8");
		localExecutionContext.setSigner(createSigner(
				paramHttpMethod.toString(), paramString1, paramString2,
				this.credentials));
		localExecutionContext.getResponseHandlers().add(
				new OSSErrorResponseHandler());
		try {
			return this.client.sendRequest(localRequestMessage,
					localExecutionContext);
		} catch (ServiceException localServiceException) {
			assert ((localServiceException instanceof OSSException));
			throw ((OSSException) localServiceException);
		}
	}

	private static RequestSigner createSigner(String paramString1,
			String paramString2, String paramString3,
			ServiceCredentials paramServiceCredentials) {
		String str = new StringBuilder()
				.append("/")
				.append(paramString2 != null ? paramString2 : "")
				.append(paramString3 != null ? new StringBuilder().append("/")
						.append(paramString3).toString() : "").toString();
		return new OSSRequestSigner(paramString1, str, paramServiceCredentials);
	}

	protected static void closeResponseSilently(
			ResponseMessage paramResponseMessage) {
		assert (paramResponseMessage != null);
		try {
			paramResponseMessage.close();
		} catch (IOException localIOException) {
		}
	}
}
